home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / c / icu-1.3.1 / icu-bin / include / fmtable.h < prev    next >
C/C++ Source or Header  |  2000-02-23  |  8KB  |  237 lines

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1999      *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  10. *                                                                              *
  11. ********************************************************************************
  12. *
  13. * File FMTABLE.H
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   02/29/97    aliu        Creation.
  19. ********************************************************************************
  20. */
  21. #ifndef FMTABLE_H
  22. #define FMTABLE_H
  23.  
  24.  
  25. #include "utypes.h"
  26. #include "unistr.h"
  27.  
  28. /**
  29.  * Formattable objects can be passed to the Format class or
  30.  * its subclasses for formatting.  Formattable is a thin wrapper
  31.  * class which interconverts between the primitive numeric types
  32.  * (double, long, etc.) as well as UDate and UnicodeString.
  33.  * <P>
  34.  * Note that this is fundamentally different from the Java behavior, since
  35.  * in this case the various formattable objects do not occupy a hierarchy,
  36.  * but are all wrapped within this one class.  Formattable encapsulates all
  37.  * the polymorphism in itself.
  38.  * <P>
  39.  * It would be easy to change this so that Formattable was an abstract base
  40.  * class of a genuine hierarchy, and that would clean up the code that
  41.  * currently must explicitly check for type, but that seems like overkill at
  42.  * this point.
  43.  */
  44. class U_I18N_API Formattable {
  45. public:
  46.     /**
  47.      * This enum is only used to let callers distinguish between
  48.      * the Formattable(UDate) constructor and the Formattable(double)
  49.      * constructor; the compiler cannot distinguish the signatures,
  50.      * since UDate is currently typedefed to be either double or long.
  51.      * If UDate is changed later to be a bonafide class
  52.      * or struct, then we no longer need this enum.
  53.      */
  54.                     enum ISDATE { kIsDate };
  55.  
  56.                     Formattable(); // Type kLong, value 0
  57.     /**
  58.      * Creates a Formattable object with a UDate instance.
  59.      * @param d the UDate instance.
  60.      * @param ISDATE the flag to indicate this is a date.
  61.      */
  62.                     Formattable(UDate d, ISDATE);
  63.     /**
  64.      * Creates a Formattable object with a double number.
  65.      * @param d the double number.
  66.      */
  67.                     Formattable(double d);
  68.     /**
  69.      * Creates a Formattable object with a long number.
  70.      * @param d the long number.
  71.      */
  72.                     Formattable(int32_t l);
  73.     /**
  74.      * Creates a Formattable object with a char string pointer.
  75.      * Assumes that the char string is null terminated.
  76.      * @param strToCopy the char string.
  77.      */
  78.                     Formattable(const char* strToCopy);
  79.     /**
  80.      * Creates a Formattable object with a UnicodeString object to copy from.
  81.      * @param strToCopy the UnicodeString string.
  82.      */
  83.                     Formattable(const UnicodeString& stringToCopy);
  84.     /**
  85.      * Creates a Formattable object with a UnicodeString object to adopt from.
  86.      * @param strToAdopt the UnicodeString string.
  87.      */
  88.                     Formattable(UnicodeString* stringToAdopt);
  89.     /**
  90.      * Creates a Formattable object with an array of Formattable objects.
  91.      * @param arrayToCopy the Formattable object array.
  92.      * @param count the array count.
  93.      */
  94.                     Formattable(const Formattable* arrayToCopy, int32_t count);
  95.  
  96.     /**
  97.      * Copy constructor.
  98.      */
  99.                     Formattable(const Formattable&);
  100.     /**
  101.      * Assignment operator.
  102.      */
  103.     Formattable&    operator=(const Formattable&);
  104.     /**
  105.      * Equality comparison.
  106.      */
  107.     bool_t          operator==(const Formattable&) const;
  108.     bool_t          operator!=(const Formattable& other) const
  109.       { return !operator==(other); }
  110.  
  111.     /** Destructor.
  112.     */
  113.     virtual         ~Formattable();
  114.  
  115.     /** 
  116.      * The list of possible data types of this Formattable object.
  117.      */
  118.     enum Type {
  119.         kDate,      // Date
  120.         kDouble,    // double
  121.         kLong,      // long
  122.         kString,    // UnicodeString
  123.         kArray      // Formattable[]
  124.     };
  125.  
  126.     /**
  127.      * Gets the data type of this Formattable object.
  128.      */
  129.     Type            getType(void) const;
  130.     
  131.     /**
  132.      * Gets the double value of this object.
  133.      */ 
  134.     double          getDouble(void) const { return fValue.fDouble; }
  135.     /**
  136.      * Gets the long value of this object.
  137.      */ 
  138.     int32_t            getLong(void) const { return fValue.fLong; }
  139.     /**
  140.      * Gets the Date value of this object.
  141.      */ 
  142.     UDate            getDate(void) const { return fValue.fDate; }
  143.  
  144.     /**
  145.      * Gets the string value of this object.
  146.      */ 
  147.     UnicodeString&  getString(UnicodeString& result) const
  148.       { result=*fValue.fString; return result; }
  149.  
  150.     /**
  151.      * Gets the array value and count of this object.
  152.      */ 
  153.     const Formattable* getArray(int32_t& count) const
  154.       { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
  155.  
  156.     /**
  157.      * Accesses the specified element in the array value of this Formattable object.
  158.      * @param index the specified index.
  159.      * @return the accessed element in the array.
  160.      */
  161.     Formattable&    operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
  162.  
  163.     /**
  164.      * Sets the double value of this object.
  165.      */ 
  166.     void            setDouble(double d);
  167.     /**
  168.      * Sets the long value of this object.
  169.      */ 
  170.     void            setLong(int32_t l);
  171.     /**
  172.      * Sets the Date value of this object.
  173.      */ 
  174.     void            setDate(UDate d);
  175.     /**
  176.      * Sets the string value of this object.
  177.      */ 
  178.     void            setString(const UnicodeString& stringToCopy);
  179.     /**
  180.      * Sets the array value and count of this object.
  181.      */ 
  182.     void            setArray(const Formattable* array, int32_t count);
  183.     /**
  184.      * Sets and adopts the string value and count of this object.
  185.      */ 
  186.     void            adoptString(UnicodeString* stringToAdopt);
  187.     /**
  188.      * Sets and adopts the array value and count of this object.
  189.      */ 
  190.     void            adoptArray(Formattable* array, int32_t count);
  191.         
  192. private:
  193.     /**
  194.      * Cleans up the memory for unwanted values.  For example, the adopted
  195.      * string or array objects.
  196.      */
  197.     void            dispose(void);
  198.  
  199.     /**
  200.      * Creates a new Formattable array and copies the values from the specified
  201.      * original.
  202.      * @param array the original array
  203.      * @param count the original array count
  204.      * @return the new Formattable array.
  205.      */
  206.     static Formattable* createArrayCopy(const Formattable* array, int32_t count);
  207.  
  208.     // Note: For now, we do not handle unsigned long and unsigned
  209.     // double types.  Smaller unsigned types, such as unsigned
  210.     // short, can fit within a long.
  211.     union {
  212.         UnicodeString*  fString;
  213.         double          fDouble;
  214.         int32_t            fLong;
  215.         UDate            fDate;
  216.         struct
  217.         {
  218.           Formattable*  fArray;
  219.           int32_t          fCount;
  220.         }               fArrayAndCount;
  221.     }                   fValue;
  222.  
  223.     Type                fType;
  224. };
  225.  
  226. inline Formattable*
  227. Formattable::createArrayCopy(const Formattable* array, int32_t count)
  228. {
  229.     Formattable *result = new Formattable[count];
  230.     for (int32_t i=0; i<count; ++i) result[i] = array[i]; // Don't memcpy!
  231.     return result;
  232. }
  233.  
  234. #endif //_FMTABLE
  235. //eof
  236.      
  237.